package com.situ.company.department.service.impl;

import java.util.List;

import com.situ.company.department.dao.IDepartmentDao;
import com.situ.company.department.dao.impl.DepartmentDao1Impl;
import com.situ.company.department.model.DepartmentModel;
import com.situ.company.department.service.IDepartmentService;
import com.situ.company.employee.dao.IEmployeeDao;
import com.situ.company.employee.dao.impl.EmployeeDaoImpl;
import com.situ.company.employee.model.EmployeeModel;

public class DepartmentServiceImpl implements IDepartmentService {

	private IDepartmentDao dao = new DepartmentDao1Impl();

	private IEmployeeDao  employeeDao = new EmployeeDaoImpl();
	@Override
	public String insert(DepartmentModel model) {
		if(selectModel(model)==null) {
			Integer res = dao.insert(model);
			return res ==null?null:res.toString();
		}
		return "repeat";
	}

	@Override
	public String delete(DepartmentModel model) {
		String codeDept = model.getCode();
		EmployeeModel employeemodel = new EmployeeModel();
		employeemodel.setCodeDept(codeDept);
		Integer count = employeeDao.selectCount(employeemodel);
		if(count>0)
			return "2";
		Integer res = dao.delete(model);
		return res ==null?null:res.toString();
		
	}

	@Override
	public String update(DepartmentModel model) {
		return dao.updateAll(model)+"";
	}

	@Override
	public List<DepartmentModel> selectList(DepartmentModel model) {
		String code = model.getCode();
		model.setCode(code==null? "%":"%"+code+"%");
		String name = model.getName();
		model.setName(name==null? "%":"%"+name+"%");
		List<DepartmentModel> list = dao.selectList(model);
		
		return dao.selectList(model);
	}     

	@Override
	public DepartmentModel selectModel(DepartmentModel model) {
		
		return dao.selectModel(new DepartmentModel(model.getCode()));
	}

	@Override
	public Integer selectCount(DepartmentModel model) {
		DepartmentModel m1 = new DepartmentModel();
		String code = model.getCode();
		m1.setCode(code==null? "%":"%"+code+"%");
		String name = model.getName();
		m1.setName(name==null? "%":"%"+name+"%");
		return dao.selectCount(m1);
		
	}
}
